package sw6.digipecs.database;

import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteCursorDriver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQuery;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class SituationCursor extends SQLiteCursor {

	private static final String[] mColumns = { DBAdapter.DB_Situation_ID, DBAdapter.DB_Situation_Name };

	private static class Factory implements CursorFactory {
		@Override
		public Cursor newCursor(SQLiteDatabase db,
				SQLiteCursorDriver masterQuery, String editTable,
				SQLiteQuery query) {
			return new SituationCursor(db, masterQuery, editTable, query);
		}
	}
	
	public static SituationCursor queryAll(SQLiteDatabase mDb) {
		return (SituationCursor) mDb.queryWithFactory(new Factory(), true, DBAdapter.DB_Situation, mColumns, null, null, null, null, null, null);
	}
	
	public static SituationCursor query(SQLiteDatabase mDb, int sid) {
		final String select  = DBAdapter.DB_Situation_ID + " = " + sid;
		return (SituationCursor) mDb.queryWithFactory(new Factory(), true, DBAdapter.DB_Situation, mColumns, select, null, null, null, null, null);
	}
	
	private int mNameIndex;
	private int mSIDIndex;
	
	public SituationCursor(SQLiteDatabase db, SQLiteCursorDriver driver,
			String editTable, SQLiteQuery query) {
		super(db, driver, editTable, query);
		mSIDIndex= getColumnIndexOrThrow(DBAdapter.DB_Situation_ID);
		mNameIndex  = getColumnIndexOrThrow(DBAdapter.DB_Situation_Name);
	}

	public String getName() {
		return getString(mNameIndex);
	}

	public int getId() {
		return getInt(mSIDIndex);
	}

}
